/* 
 * Copyright 2011 Carnegie Mellon University
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package edu.cmu.lti.dimple.data;

import java.io.File;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import edu.cmu.lti.dimple.FileUtils;

/**
 * Create (system response, answer nugget) pairs with a binary label, 
 * given TREC complex qa nugget judgment files.  
 * 
 * @author Hideki Shima
 *
 */
public class ComplexQAEvalPairLoader {

  public static List<ComplexQAEvalPair> load(File judgmentDir, File nugget) {
    List<ComplexQAEvalPair> result = new ArrayList<ComplexQAEvalPair>();
    
    //2007other
    //N  216.8 1 0 Ben Bernanke hired Krugman to teach at Princeton
    //SR 216.8 RUN-1-2 1 APW_ENG_20060123.0008 faulted Greenspan for ..
    //J  266.10 RUN-1-1 5 3
    
    //2007ciqa
    //N  56 1 0.833333333333333 Croat
    //SR 56 RUN-22-1 3 AFP_ENG_20060306.0378 Police
    //J  56 RUN-22-1 1 1
    
    //2006ciqa
    //N  26 1[TAB]vital[TAB]VCDs 
    //SR 26 RUN-1 2 XIE20000623.0202 police foiled 
    //J  26 RUN-1 2 6

    Pattern pN  = Pattern.compile("^([0-9]+\\.?[0-9]+)\\s([0-9]+)\\s([^ ]+)\\s(.+)");
    Pattern pSR = Pattern.compile("^([0-9]+\\.?[0-9]+) (RUN[^ ]+) ([0-9]+) ([^ ]+) (.+)");
    Pattern pJ  = Pattern.compile("^([0-9]+\\.?[0-9]+) (RUN[^ ]+) ([0-9]+) ([0-9]+)$");
    
    Map<String,String> nuggets = new LinkedHashMap<String,String>();
    String[] nContents = FileUtils.readFile(nugget).split("\\n");
    for ( String line : nContents ) {
      Matcher m = pN.matcher(line);
      while (m.find()) {
        nuggets.put(m.group(1)+" "+m.group(2), m.group(4)); 
      }
    }
    
    Map<String,String> srs = new LinkedHashMap<String,String>();
    File[] jfiles = judgmentDir.listFiles();
    for ( File j : jfiles ) {
      if (j.isHidden()) continue;
      String[] jContent = FileUtils.readFile(j).split("\\n");
      for ( String line : jContent ) {
        Matcher m = pSR.matcher(line);
        while (m.find()) {
          String key = m.group(1)+" "+m.group(2)+" "+m.group(3);
          srs.put(key, m.group(5));
        }        
      }
      for ( String line : jContent ) {
        Matcher m = pJ.matcher(line);
        while (m.find()) {
          String srKey = m.group(1)+" "+m.group(2)+" "+m.group(3);
          String nKey = m.group(1)+" "+m.group(4);
          String jKey = m.group(1)+" "+m.group(2)+" "+m.group(3)+" "+m.group(4);
          result.add( new ComplexQAEvalPair(jKey, "1", srs.get(srKey), nuggets.get(nKey)) );          
        }
      }
    }
    
    return result;
  }
}
